package com.goambu.driver.framework.controller.internet;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.goambu.driver.GoAmbuDriverConstants;
import com.goambu.driver.R;
import com.goambu.driver.autenticacion.controller.AutenticacionController;
import com.goambu.driver.autenticacion.view.ApproveReceiver;
import com.goambu.driver.autenticacion.view.MainActivity;
import com.goambu.driver.autenticacion.view.SplashActivity;
import com.google.android.gms.gcm.GoogleCloudMessaging;

public class GCMNotificationIntentService extends IntentService {
	// Sets an ID for the notification, so it can be updated
	
	NotificationCompat.Builder builder;
	Context context;
	
	public GCMNotificationIntentService() {
		super("GcmIntentService");
		context = this;
	}

	public GCMNotificationIntentService(Context context) {
		super("GcmIntentService");
		this.context = context;
	}
	
	@Override
	protected void onHandleIntent(Intent intent) {
		Bundle extras = intent.getExtras();
		GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
		Log.d("GCMNotificationIntentService", "onHandleIntent");
		String contenidoPush = extras.getString("AIzaSyDWEswpKgFAs_RW9cWSkY4bXwq6vkQM4UM");
		//String contenidoPush = "contenido";
		if (contenidoPush!=null){
			Log.d("contenidoPush", contenidoPush);
		}
		String messageType = gcm.getMessageType(intent);

		if (!extras.isEmpty()) {
			if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
					.equals(messageType)) {
				sendNotification("Send error: " + extras.toString(), contenidoPush);
			} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
					.equals(messageType)) {
				sendNotification("Deleted messages on server: "
						+ extras.toString(),contenidoPush);
			} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
					.equals(messageType)) {
				Log.d("GCMNotificationIntentService", "Message Received from Google GCM Server");
				sendNotification("Message Received from Google GCM Server:\n\n"
						+ extras.get(GoAmbuDriverConstants.MSG_KEY),contenidoPush);
			}
		}
		GcmBroadcastReceiver.completeWakefulIntent(intent);
		
	}

	public void sendNotification2(String msg, String contenidoPush) {
		Log.d("GCMNotificationIntentService", "Alerta Recibida");
		Intent popupReceive;
		Intent yesReceive;
		Intent noReceive;
		
		if (AutenticacionController.getInstance().currentUser==null){
			popupReceive = new Intent(context, SplashActivity.class);
			yesReceive = new Intent(context, SplashActivity.class);
			noReceive = new Intent(context, SplashActivity.class);
		}
		else {
			popupReceive = new Intent(context, MainActivity.class);
			yesReceive = new Intent(context, MainActivity.class);
			noReceive = new Intent(context, MainActivity.class);
		}
		
		
		//PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0,
		//		resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 12345,
		popupReceive, PendingIntent.FLAG_CANCEL_CURRENT);
		
		popupReceive.setAction(GoAmbuDriverConstants.NOTIFICACION_POPUP_ACTION);
		popupReceive.putExtra("msg", contenidoPush);
		
		//Yes intent
		yesReceive.setAction(GoAmbuDriverConstants.NOTIFICACION_YES_ACTION);
		
		PendingIntent pendingIntentYes = PendingIntent.getActivity(context, 12345, yesReceive, PendingIntent.FLAG_CANCEL_CURRENT);
		

		//No intent
		noReceive.setAction(GoAmbuDriverConstants.NOTIFICACION_NO_ACTION);
		PendingIntent pendingIntentNo = PendingIntent.getActivity(context, 12345, noReceive, PendingIntent.FLAG_CANCEL_CURRENT);
		
		NotificationCompat.Builder mNotifyBuilder;
		NotificationManager mNotificationManager;

		mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

		mNotifyBuilder = new NotificationCompat.Builder(context)
				.setContentTitle("Alerta recibida")
				.setContentText(contenidoPush)
				.setSmallIcon(R.drawable.logo_goambu_sinfondo_mini)
				.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.alertapush));
		
		mNotifyBuilder.addAction(R.drawable.check2, "Aprobar", pendingIntentYes);
		mNotifyBuilder.addAction(R.drawable.reject2, "Rechazar", pendingIntentNo);
		
		
		// Set pending intent
		mNotifyBuilder.setContentIntent(resultPendingIntent);

		// Set Vibrate, Sound and Light
		int defaults = 0;
		defaults = defaults | Notification.DEFAULT_LIGHTS;
		defaults = defaults | Notification.DEFAULT_VIBRATE;
		defaults = defaults | Notification.DEFAULT_SOUND;

		mNotifyBuilder.setDefaults(defaults);
		// Set the content for Notification
		mNotifyBuilder.setContentText(contenidoPush);
		// Set autocancel
		mNotifyBuilder.setAutoCancel(true);
		// Post a notification
		mNotificationManager.notify(GoAmbuDriverConstants.notifyID, mNotifyBuilder.build());
	}
	
	public void sendNotification3(String msg, String contenidoPush) {
		Log.d("GCMNotificationIntentService", "Alerta Recibida");
		Intent popupReceive;
		Intent yesReceive;
		Intent noReceive;
		
		
		popupReceive = new Intent();
		yesReceive =  new Intent();
		noReceive = new Intent();
	
		
		
		PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 1,
				popupReceive, PendingIntent.FLAG_UPDATE_CURRENT);
		/*PendingIntent resultPendingIntent = PendingIntent.getBroadcast(this, 0,
		popupReceive, PendingIntent.FLAG_UPDATE_CURRENT);*/
		
		popupReceive.setAction(GoAmbuDriverConstants.NOTIFICACION_POPUP_ACTION);
		popupReceive.putExtra("msg", contenidoPush);
		
		//Yes intent
		yesReceive.setAction(GoAmbuDriverConstants.NOTIFICACION_YES_ACTION);
		PendingIntent pendingIntentYes = PendingIntent.getActivity(this, 1, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
		
		//No intent
		noReceive.setAction(GoAmbuDriverConstants.NOTIFICACION_NO_ACTION);
		PendingIntent pendingIntentNo = PendingIntent.getActivity(this, 1, noReceive, PendingIntent.FLAG_UPDATE_CURRENT);
		
		NotificationCompat.Builder mNotifyBuilder;
		NotificationManager mNotificationManager;

		mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

		mNotifyBuilder = new NotificationCompat.Builder(this)
				.setContentTitle("Alerta recibida")
				.setContentText(contenidoPush)
				.setSmallIcon(R.drawable.logo_goambu_sinfondo_mini)
				.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.alertapush));
		
		mNotifyBuilder.addAction(R.drawable.check2, "Aprobar", pendingIntentYes);
		mNotifyBuilder.addAction(R.drawable.reject2, "Rechazar", pendingIntentNo);
		
		
		// Set pending intent
		mNotifyBuilder.setContentIntent(resultPendingIntent);

		// Set Vibrate, Sound and Light
		int defaults = 0;
		defaults = defaults | Notification.DEFAULT_LIGHTS;
		defaults = defaults | Notification.DEFAULT_VIBRATE;
		defaults = defaults | Notification.DEFAULT_SOUND;

		mNotifyBuilder.setDefaults(defaults);
		// Set the content for Notification
		mNotifyBuilder.setContentText(contenidoPush);
		// Set autocancel
		mNotifyBuilder.setAutoCancel(true);
		// Post a notification
		mNotificationManager.notify(GoAmbuDriverConstants.notifyID, mNotifyBuilder.build());
	}

	public void sendNotification4(String msg, String contenidoPush) {
		Log.d("GCMNotificationIntentService", "Alerta Recibida");
		Intent popupReceive;	
		Intent yesReceive;
		Intent noReceive;
		
		
		popupReceive = new Intent(this, MainActivity.class);
		popupReceive.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		yesReceive = new Intent(this, MainActivity.class);
		yesReceive.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		noReceive = new Intent(this, MainActivity.class);
		noReceive.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		
		PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 2,
				popupReceive, PendingIntent.FLAG_UPDATE_CURRENT);
		/*PendingIntent resultPendingIntent = PendingIntent.getBroadcast(this, 0,
		popupReceive, PendingIntent.FLAG_UPDATE_CURRENT);*/
		
		popupReceive.setAction(GoAmbuDriverConstants.NOTIFICACION_POPUP_ACTION);
		popupReceive.putExtra("accion", GoAmbuDriverConstants.NOTIFICACION_POPUP_ACTION);
		popupReceive.putExtra("msg", contenidoPush);
		
		//Yes intent
		yesReceive.setAction(GoAmbuDriverConstants.NOTIFICACION_YES_ACTION);
		yesReceive.putExtra("accion", GoAmbuDriverConstants.NOTIFICACION_YES_ACTION);
		PendingIntent pendingIntentYes = PendingIntent.getActivity(this, 3, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
		
		
		//No intent
		noReceive.setAction(GoAmbuDriverConstants.NOTIFICACION_NO_ACTION);
		yesReceive.putExtra("accion", GoAmbuDriverConstants.NOTIFICACION_NO_ACTION);
		PendingIntent pendingIntentNo = PendingIntent.getActivity(this, 4, noReceive, PendingIntent.FLAG_UPDATE_CURRENT);
				
				
				
		NotificationCompat.Builder mNotifyBuilder;
		NotificationManager mNotificationManager;

		mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

		mNotifyBuilder = new NotificationCompat.Builder(this)
				.setContentTitle("Alerta recibida")
				.setContentText(contenidoPush)
				.setSmallIcon(R.drawable.logo_goambu_sinfondo_mini)
				.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.alertapush));
		mNotifyBuilder.addAction(R.drawable.check2, "Aprobar", pendingIntentYes);
		mNotifyBuilder.addAction(R.drawable.reject2, "Rechazar", pendingIntentNo);
		
		// Set pending intent
		mNotifyBuilder.setContentIntent(resultPendingIntent);

		// Set Vibrate, Sound and Light
		int defaults = 0;
		defaults = defaults | Notification.DEFAULT_LIGHTS;
		defaults = defaults | Notification.DEFAULT_VIBRATE;
		defaults = defaults | Notification.DEFAULT_SOUND;

		mNotifyBuilder.setDefaults(defaults);
		// Set the content for Notification
		mNotifyBuilder.setContentText(contenidoPush);
		// Set autocancel
		mNotifyBuilder.setAutoCancel(true);
		// Post a notification
		mNotificationManager.notify(GoAmbuDriverConstants.notifyID, mNotifyBuilder.build());
	}

	public void sendNotification(String msg, String contenidoPush) {
		Log.d("GCMNotificationIntentService", "Alerta Recibida");
		Intent popupReceive;	
		Intent yesReceive;
		Intent noReceive;
		
		if (AutenticacionController.getInstance().currentUser==null){
			popupReceive = new Intent(this, SplashActivity.class);
			yesReceive = new Intent(this, SplashActivity.class);
			noReceive = new Intent(this, SplashActivity.class);
		}
		else {
			popupReceive = new Intent(this, MainActivity.class);
			yesReceive = new Intent(this, MainActivity.class);
			noReceive = new Intent(this, MainActivity.class);		
		}
		
		
		popupReceive.setAction(GoAmbuDriverConstants.NOTIFICACION_POPUP_ACTION);
		popupReceive.putExtra("accion", GoAmbuDriverConstants.NOTIFICACION_POPUP_ACTION);
		popupReceive.putExtra("msg", contenidoPush);
		
		//Yes intent
		yesReceive.setAction(GoAmbuDriverConstants.NOTIFICACION_YES_ACTION);
		yesReceive.putExtra("accion", GoAmbuDriverConstants.NOTIFICACION_YES_ACTION);
		
		
		//No intent
		noReceive.setAction(GoAmbuDriverConstants.NOTIFICACION_NO_ACTION);
		yesReceive.putExtra("accion", GoAmbuDriverConstants.NOTIFICACION_NO_ACTION);
		
		
		PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 2,
				popupReceive, PendingIntent.FLAG_UPDATE_CURRENT);
		PendingIntent pendingIntentYes = PendingIntent.getActivity(this, 3, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
		PendingIntent pendingIntentNo = PendingIntent.getActivity(this, 4, noReceive, PendingIntent.FLAG_UPDATE_CURRENT);
		/*PendingIntent resultPendingIntent = PendingIntent.getBroadcast(this, 0,
		popupReceive, PendingIntent.FLAG_UPDATE_CURRENT);*/
				
				
		NotificationCompat.Builder mNotifyBuilder;
		NotificationManager mNotificationManager;

		mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

		mNotifyBuilder = new NotificationCompat.Builder(this)
				.setContentTitle("Alerta recibida")
				.setContentText(contenidoPush)
				.setSmallIcon(R.drawable.logo_goambu_sinfondo_mini)
				.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.alertapush));
		mNotifyBuilder.addAction(R.drawable.check2, "Aprobar", pendingIntentYes);
		mNotifyBuilder.addAction(R.drawable.reject2, "Rechazar", pendingIntentNo);
		
		// Set pending intent
		mNotifyBuilder.setContentIntent(resultPendingIntent);

		// Set Vibrate, Sound and Light
		int defaults = 0;
		defaults = defaults | Notification.DEFAULT_LIGHTS;
		defaults = defaults | Notification.DEFAULT_VIBRATE;
		defaults = defaults | Notification.DEFAULT_SOUND;

		mNotifyBuilder.setDefaults(defaults);
		// Set the content for Notification
		mNotifyBuilder.setContentText(contenidoPush);
		// Set autocancel
		mNotifyBuilder.setAutoCancel(true);
		// Post a notification
		mNotificationManager.notify(GoAmbuDriverConstants.notifyID, mNotifyBuilder.build());
	}

}
